#pragma once
#include<iostream>
using namespace std;

enum Color
{
	RED,
	BLACK
};

template<class V>
struct RBNode
{
	RBNode<V>* _left;
	RBNode<V>* _right;
	RBNode<V>* _parent;
	
	Color _col;

	V _kv;

	RBNode(const V& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_col(RED)
		,_kv(kv)
	{}
};


template<class V>
struct _Iterator
{
	typedef RBNode<V> Node;
	typedef _Iterator<V> Self;

	Node* _node;

	_Iterator(Node* node)
		:_node(node)
	{}

	V& operator*()
	{
		return _node->_kv;
	}

	V* operator->()
	{
		return &(_node->_kv);
	}

	Self& operator++()
	{
		if (_node->_right)
		{
			Node* cur = _node->_right;
			while (cur->left)
			{
				cur = cur->_left;
			}
			_node = cur;
		}
		else
		{
			Node* cur = _node;
			Node* parent = _node->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return _node;
	}

	Self& operator--()
	{
		
		Node* cur = _node;
		Node* parent = _node->_parent;
		if (cur->_left)
		{
			Node* cur = _node->left;
			while (cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		else
		{
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = parent->_right;
			}
			_node = parent;
		}
		return _node;
	}

	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}

	bool operator==(const Self& s)
	{
		return _node == s._node;
	}

};

template<class K,class V, class KeySet>
class RBTree
{
	typedef RBNode<V> Node;
	typedef _Iterator<V> iterator;
public:
	pair<iterator,bool> Insert(const V& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return make_pair(_root, true);
		}
		Node* cur = _root;
		Node* parent = nullptr;
		KeySet kot;

		while (cur)
		{
			if (kot(cur->_kv) > kot(kv))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kot(cur->_kv) < kot(kv))
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return make_pair(cur, false);
			}
		}

		cur = new Node(kv);

		if (parent->_left == cur)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		if (parent->_col == BLACK)
		{
			return make_pair(cur, true);
		}

		while (parent&&parent->_col==RED)
		{
			Node* pparent = parent->_parent;
			if (parent == pparent->_left)
			{
				Node* uncle = pparent->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					pparent->_col = RED;
					cur = pparent;
					parent = pparent->_parent;
				}
				else if(uncle==nullptr||uncle->_col==BLACK)
				{
					if (cur == parent->_left)
					{
						RotateR(pparent);
						pparent->_col = RED;
						parent->_col = BLACK;
					}
					else 
					{
						RotateL(parent);
						RotateR(pparent);
						cur->_col = BLACK;
						pparent->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = pparent->_left;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					pparent->_col = RED;

					cur = pparent;
					parent = pparent->_parent;
				}
				else if (uncle == nullptr || uncle->_col == BLACK)
				{
					if (cur == parent->_left)
					{
						RotateR(parent);
						RoatatL(pparent);
						cur->_col = BLACK;
						pparent->_col = RED;
					}
					else
					{
						RotateL(pparent);
						parent->_col = BLACK;
						pparent->_col = RED;
					}
					break;
				}
			}
		}

		_root->_col = BLACK;

		return make_pair(iterator(cur), true);

	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		Node* pparent = parent->_parent;

		parent->_right = subRL;
		subR->_left = parent;

		parent->_parent = subR;
		if (subRL)
			subRL->_parent = parent;

		if (_root == parent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subR;
			}
			else
			{
				pparent->_right = subR;
			}
			subR->_parent = pparent;
		}
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		Node* pparent = parent->_parent;

		subL->_right = parent;
		parent->_left = subLR;

		parent->_parent = subL;
		if (subLR)
			subLR->_parent = parent;

		if (_root == parent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subL;
			}
			else
			{
				pparent->_right = subL;
			}
			subL->_parent = pparent;
		}
	}

	iterator begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return iterator(cur);
	}

	iterator end()
	{
		return iterator(nullptr);
	}
private:
	Node* _root;
};





